<!-- 开卡补卡 -->
<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--企业数据-->
            <el-col :span="4" :xs="24">
                <div class="head-container">
                    <el-input v-model="deptName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
                        style="margin-bottom: 20px" />
                </div>
                <div class="head-containers">
                    <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                        :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
                        @node-click="handleNodeClick" />
                </div>
            </el-col>
            <!--小区数据-->
            <el-col :span="20" :xs="24">
                <el-card class="box-card">
                    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                        label-width="68px">
                        <el-form-item label="客户类型" prop="cons_type">
                            <el-select v-model="queryParams.cons_type" filterable placeholder="请选择客户类型" style="width:200px">
                                <el-option label="工业" value="industrial"> </el-option>
                                <el-option label="商业" value="commercial"> </el-option>
                                <el-option label="民用" value="civil"> </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="小区名称" prop="userName">
                            <el-input v-model="queryParams.userName" placeholder="请输入小区名称" clearable style="width: 200px"
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="营业点" prop="status">
                            <el-select v-model="queryParams.nickName" filterable placeholder="请选择">
                                <el-option label="营业点一" value="1"> </el-option>
                                <el-option label="营业点二" value="2"> </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="手机号" prop="userName">
                            <el-input v-model="queryParams.userName" type="number" placeholder="请输入手机号" clearable
                                style="width: 200px" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>

                    <!-- <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                                v-hasPermi="['fileManagement:civilArchives:add']">新增</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                                @click="handleUpdate" v-hasPermi="['fileManagement:civilArchives:edit']">修改</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                                @click="handleDelete" v-hasPermi="['fileManagement:civilArchives:remove']">删除</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                                v-hasPermi="['fileManagement:civilArchives:import']">导入</el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                                v-hasPermi="['fileManagement:civilArchives:export']">导出</el-button>
                        </el-col>
                        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                    </el-row> -->

                    <el-table v-loading="loading" :data="userList" border @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="50" align="center" />
                        <el-table-column label="客户编号" align="center" key="userName" prop="userName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="姓名" align="center" key="userName" prop="userName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="联系方式" align="center" key="userName" prop="userName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="表具编号" align="center" key="userName" prop="userName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="策略模板" align="center" key="deptName" prop="dept.deptName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="客户类型" align="center" key="deptName" prop="dept.deptName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="安装地址" align="center" key="deptName" prop="dept.deptName"
                            :show-overflow-tooltip="true" width="100" />
                        <el-table-column label="发卡状态" align="center" key="deptName" prop="dept.deptName"
                            :show-overflow-tooltip="true" width="100" />
                        <el-table-column label="用气状态" align="center" key="deptName" prop="dept.deptName"
                            :show-overflow-tooltip="true" />
                        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"
                            fixed="right">
                            <template slot-scope="scope">
                                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                                    v-hasPermi="['fileManagement:civilArchives:edit']">补卡</el-button>
                                <el-button size="mini" type="text" icon="el-icon-tickets" @click="handlAaddBj(scope.row)"
                                    v-hasPermi="['fileManagement:civilArchives:addBj']">开卡</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize" @pagination="getList" />
                </el-card>

            </el-col>
        </el-row>


        <!-- 新增表计弹框 -->
        <el-dialog :title="title2" :visible.sync="bjOPen" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <div class="title">
                        <div class="title_box">
                            <div class="title_icon"></div>
                            <div class="title_value">卡表参数</div>
                        </div>
                    </div>
                    <el-col :span="24">
                        <el-form-item label="卡表参数选择" prop="nickName">
                            <el-select v-model="form.nickName" filterable placeholder="请选择" style="width:50%">
                                <el-option label="新户" value="1"> </el-option>
                                <el-option label="技改户" value="2"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <div class="title">
                        <div class="title_box">
                            <div class="title_icon"></div>
                            <div class="title_value">费用项收取</div>
                        </div>
                    </div>
                    <el-col :span="8">
                        <el-form-item label="收费类型" prop="nickName">
                            <el-select v-model="form.nickName" filterable placeholder="请选择">
                                <el-option label="类型一" value="1"> </el-option>
                                <el-option label="类型二" value="2"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="金额(元)" prop="nickName">
                            <el-input v-model="form.nickName" placeholder="请输入金额" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="14">
                        <el-form-item label="应收总额" prop="nickName">
                            <el-input v-model="form.nickName" type="number" placeholder="0.00" maxlength="30" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="14">
                        <el-form-item label="实收金额(元)" prop="nickName">
                            <el-input v-model="form.nickName" type="number" placeholder="请输入实收金额" maxlength="30" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="14">
                        <el-form-item label="支付方式" prop="nickName">
                            <el-select v-model="form.nickName" filterable placeholder="请选择" style="width:100%">
                                <el-option label="现金" value="1"> </el-option>
                                <el-option label="微信" value="2"> </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="14">
                        <el-form-item label="找零金额(元)" prop="nickName">
                            <el-input v-model="form.nickName" type="number" placeholder="0.00" maxlength="30" disabled />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm2">开卡</el-button>
                <el-button @click="cancel2">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import {
    listUser,
    getUser,
    delUser,
    addUser,
    updateUser,
    deptTreeSelect,
} from "@/api/system/user";
import { getToken } from "@/utils/auth";
import { areaTreeList } from "@/api/fileManagement/areainfo.js"

export default {
    name: "civilArchives",
    components: {},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 小区表格数据
            userList: null,
            // 弹出层标题
            title: "",
            //表计弹出层标题
            title2: "",
            // 企业树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 企业名称
            deptName: undefined,
            //是否表计显示弹出层
            bjOPen: false,
            // 表单参数
            form: {},
            defaultProps: {
                children: "children",
                label: "areaName",
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                phonenumber: undefined,
                status: undefined,
                deptId: undefined,
            },
            // 表单校验
            rules: {
                userName: [
                    { required: true, message: "小区名称不能为空", trigger: "blur" },
                    {
                        min: 2,
                        max: 20,
                        message: "小区名称长度必须介于 2 和 20 之间",
                        trigger: "blur",
                    },
                ],
                nickName: [
                    { required: true, message: "小区昵称不能为空", trigger: "blur" },
                ],
            },
            activeNames: ["1"],
        };
    },
    watch: {
        // 根据名称筛选企业树
        deptName(val) {
            this.$refs.tree.filter(val);
        },
    },
    created() {
        this.getList();
        this.getDeptTree();
    },
    methods: {
        /** 查询小区列表 */
        getList() {
            this.loading = true;
            listUser(this.addDateRange(this.queryParams)).then((response) => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询企业下拉树结构 */
        getDeptTree() {
            areaTreeList().then((response) => {
                this.deptOptions = response.rows;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.handleQuery();
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表计取消按钮
        cancel2() {
            this.bjOPen = false;
        },
        // 表单重置
        reset() {
            this.form = {
                userId: undefined,
                deptId: undefined,
                userName: undefined,
                nickName: undefined,
                password: undefined,
                phonenumber: undefined,
                email: undefined,
                sex: undefined,
                status: "0",
                remark: undefined,
                postIds: [],
                roleIds: [],
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.deptId = undefined;
            this.$refs.tree.setCurrentKey(null);
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.userId);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            getUser().then((response) => {
                this.postOptions = response.posts;
                this.roleOptions = response.roles;
                this.open = true;
                this.title = "添加民用档案";
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const userId = row.userId || this.ids;
            getUser(userId).then((response) => {
                this.form = response.data;
                this.postOptions = response.posts;
                this.roleOptions = response.roles;
                this.$set(this.form, "postIds", response.postIds);
                this.$set(this.form, "roleIds", response.roleIds);
                this.bjOPen = true;
                this.title = "客户补卡";
                this.form.password = "";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.userId != undefined) {
                        updateUser(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addUser(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        //表计提交按钮
        submitForm2() {
            this.bjOPen = false;
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const userIds = row.userId || this.ids;
            this.$modal
                .confirm('是否确认删除小区编号为"' + userIds + '"的数据项？')
                .then(function () {
                    return delUser(userIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        handlAaddBj(row) {
            this.bjOPen = true;
            this.title2 = "初始发卡";
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/user/export",
                {
                    ...this.queryParams,
                },
                `user_${new Date().getTime()}.xlsx`
            );
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "小区导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download(
                "system/user/importTemplate",
                {},
                `user_template_${new Date().getTime()}.xlsx`
            );
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                response.msg +
                "</div>",
                "导入结果",
                { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
    },
};
</script>
<style lang="scss">
.head-containers {
    width: 200px;
    overflow: auto;

    .el-tree {
        min-width: 100%;
        display: inline-block !important;
    }
}

.title {
    height: 40px;
    color: #303133;
    font-weight: 700;
    line-height: 28px;
    padding-left: 10px;
    display: flex;
    padding-left: 10px;
    justify-content: space-between;
    margin-bottom: 30px;
    align-items: center;
    background-color: #F5F7FA;
    border-radius: 5px;

    .title_box {
        width: 100%;
        display: flex;
        align-items: center;

        .title_icon {
            width: 4px;
            height: 18px;
            background-color: #d26454;
            border-radius: 4px;
            margin-right: 10px;
        }

        .title_notes {
            font-weight: 500;
            margin-left: 20px;
        }
    }


}

.dialog-footer {
    text-align: center
}
</style>
  